class TheFallenMight_HUD extends HUD;

/*	 ================================================
 *   ===============   variables   ==================
 *   ================================================
 */
 var MouseInterfacePlayerInput MouseInterfacePlayerInput;

 /*	 ================================================
 *   ===============   Functions   ==================
 *   ================================================
 */
 /**
 * This function will fetch mouse coordinates from the UI
 * Hierarchy in the UIController of the PlayerController
 */
function vector2D GetMouseCoordinates()
{
        local Vector2D MousePosition;

  // Ensure that we have a valid PlayerOwner
  if (PlayerOwner != None) 
  {
    // Cast to get the MouseInterfacePlayerInput
    MouseInterfacePlayerInput = MouseInterfacePlayerInput(PlayerOwner.PlayerInput); 

    if (MouseInterfacePlayerInput != None)
    {
      // To retrieve/use the mouse X position
      MousePosition.X = MouseInterfacePlayerInput.MousePosition.X;
      // To retrieve/use the mouse Y position
      MousePosition.Y = MouseInterfacePlayerInput.MousePosition.Y;
    }
  }
  return MousePosition;
}

/**
 * Draw a text with a border. Useful for drawing white text with a black background to make it stand out all the time. This function
 * is quite expensive on mobile hardware such as the iOS, so you use sparingly.
 *
 * @param		HUD				HUD to draw the text on
 * @param		PositionX		X position on the canvas to draw the text
 * @param		PositionY		Y position on the canvas to draw the text
 * @param		TextColor		Color to draw the text in
 * @param		BorderColor		Color to draw the border in
 */
final static function DrawBorderedText(HUD HUD, int PositionX, int PositionY, String Text, Color TextColor, Color BorderColor)
{
	local FontRenderInfo FontRenderInfo;

	if (HUD == None || HUD.Canvas == None || Text ~= "")
	{
		return;
	}

	FontRenderInfo.bClipText = true;
	HUD.Canvas.DrawColor = BorderColor;
	// Top border
	HUD.Canvas.SetPos(PositionX - 1, PositionY - 1);
	HUD.Canvas.DrawText(Text,,,, FontRenderInfo);
	HUD.Canvas.SetPos(PositionX, PositionY - 1);
	HUD.Canvas.DrawText(Text,,,, FontRenderInfo);
	HUD.Canvas.SetPos(PositionX + 1, PositionY - 1);
	HUD.Canvas.DrawText(Text,,,, FontRenderInfo);
	// Middle border
	HUD.Canvas.SetPos(PositionX - 1, PositionY);
	HUD.Canvas.DrawText(Text,,,, FontRenderInfo);
	HUD.Canvas.SetPos(PositionX + 1, PositionY);
	HUD.Canvas.DrawText(Text,,,, FontRenderInfo);
	// Bottom border
	HUD.Canvas.SetPos(PositionX - 1, PositionY + 1);
	HUD.Canvas.DrawText(Text,,,, FontRenderInfo);
	HUD.Canvas.SetPos(PositionX, PositionY + 1);
	HUD.Canvas.DrawText(Text,,,, FontRenderInfo);
	HUD.Canvas.SetPos(PositionX + 1, PositionY + 1);
	HUD.Canvas.DrawText(Text,,,, FontRenderInfo);
	// Render the text
	HUD.Canvas.SetPos(PositionX, PositionY);
	HUD.Canvas.DrawColor = TextColor;
	HUD.Canvas.DrawText(Text,,,, FontRenderInfo);
}
/*******************************************************************
 *  Helper trace for you to visually see where collision and tracing
 *  extend to.
 *******************************************************************/
function DrawTraceDebugRays()
{
        local TheFallenMight_PlayerController TheFallenMight_PlayerController;
        TheFallenMight_PlayerController = TheFallenMight_PlayerController(PlayerOwner);

        //Draw Trace from the camera to the world using
        Draw3DLine(TheFallenMight_PlayerController.StartTrace, TheFallenMight_PlayerController.EndTrace, MakeColor(255,128,128,255));

        /**
         * Draw eye ray for collision and determine if a clear running is permitted(no obstacles between pawn && destination)
         * @fixme:Each pawn in the world.
         */
		//Draw3DLine(TheFallenMight_PlayerController.PawnEyeLocation, TheFallenMight_PlayerController.MouseHitWorldLocation, MakeColor(0,200,255,255));
}

/**
 * This is the main drawing pump.  It will determine which hud we need to draw (Game or PostGame).  Any drawing that should occur
 * regardless of the game state should go here.
 */
function DrawHUD()
{
        //local Vector Direction;
        local string StringMessage;

        //Display traced actor class under mouse cursor for fun  
        if(TheFallenMight_PlayerController(PlayerOwner).TraceActor != none)
        {
                StringMessage = "Actor selected:"@TheFallenMight_PlayerController(PlayerOwner).TraceActor.class;
        }

        // now draw string with GoldColor color defined in defaultproperties. note you can
        // alternatively use MakeColor(R,G,B,A)
        Canvas.DrawColor = Makecolor(0,0,0,255);
        Canvas.SetPos( 250, 10 );
        Canvas.DrawText( StringMessage, false);
}

event PostRender()
{
	local TheFallenMight_Camera TheFallenMight_Camera;
	local TheFallenMight_PlayerController TheFallenMight_PlayerController;
	super.PostRender();
	if(PlayerOwner != none)
	{
		TheFallenMight_PlayerController=TheFallenMight_PlayerController(PlayerOwner);
		TheFallenMight_Camera=TheFallenMight_Camera(TheFallenMight_PlayerController.PlayerCamera);
		TheFallenMight_PlayerController.PlayerMouse = GetMouseCoordinates();
		//Deproject the 2d mouse coordinate into 3d world. Store the MousePosWorldLocation and normal (direction).
		Canvas.DeProject(TheFallenMight_PlayerController.PlayerMouse, TheFallenMight_PlayerController.MousePosWorldLocation, TheFallenMight_PlayerController.MousePosWorldNormal);
		//Set the ray direction as the mouseWorldnormal
		TheFallenMight_PlayerController.RayDir = TheFallenMight_PlayerController.MousePosWorldNormal;
		//Start the trace at the player camera (isometric) + 100 unit z and a little offset in front of the camera (direction *10)
		TheFallenMight_PlayerController.StartTrace =  TheFallenMight_Camera.ViewTarget.POV.Location + TheFallenMight_PlayerController.rayDir;
		//End this ray at start + the direction multiplied by given distance (5000 unit is far enough generally)
		TheFallenMight_PlayerController.EndTrace = TheFallenMight_PlayerController.StartTrace + TheFallenMight_PlayerController.rayDir * 65536;
		//Trace MouseHitWorldLocation each frame to world location (here you can get from the trace the actors that are hit by the trace, for the sake of this
        //simple tutorial, we do noting with the result, but if you would filter clicks only on terrain, or if the player clicks on an npc, you would want to inspect
        //the object hit in the StartFire function
        TheFallenMight_PlayerController.TraceActor = Trace(TheFallenMight_PlayerController.MouseHitWorldLocation, TheFallenMight_PlayerController.MouseHitWorldNormal, TheFallenMight_PlayerController.EndTrace, TheFallenMight_PlayerController.StartTrace, true);
		
        /**
         * 
         * Calculate the pawn eye location for debug ray and for checking obstacles on click.
         * @fixme:for each pawn.
         */
        //IsoPlayerController.PawnEyeLocation = Pawn(PlayerOwner.ViewTarget).Location + Pawn(PlayerOwner.ViewTarget).EyeHeight * vect(0,0,1);



		if (MouseInterfacePlayerInput != None)
		{
			Canvas.DrawColor = Makecolor(0,0,0,255);
			Canvas.SetPos(MouseInterfacePlayerInput.MousePosition.X,MouseInterfacePlayerInput.MousePosition.Y);
			Canvas.DrawText("Mouse Position: X:"$MouseInterfacePlayerInput.MousePosition.X$" Y: "$MouseInterfacePlayerInput.MousePosition.X, false);
		}
		if(TheFallenMight_PlayerController.bDrawTraces)
		{
			//If display is enabled from console, then draw Pathfinding routes and rays.
			//super.DrawRoute(Pawn(PlayerOwner.ViewTarget));
			DrawTraceDebugRays();
		}


        //Your basic draw hud routine
        DrawHUD();
	}
}


DefaultProperties
{
}
